<template>
    <div>
      <el-card>
        <div slot="header">编辑文章</div>
        <div class="card-c">
  <el-form :model="addForm"  ref="addForm" label-width="100px" class="demo-ruleForm" :rules="rules">
  <el-row :gutter="10">
    <el-col :span="8">
       <el-form-item label="作者" prop="author">
    <el-input v-model="addForm.author" size="mini"></el-input>
  </el-form-item>
    </el-col>
    <el-col :span="8">
       <el-form-item label="标题" prop="title">
    <el-input  size="mini" v-model="addForm.title"></el-input>
  </el-form-item>
    </el-col>
    <el-col :span="8">
       <el-form-item label="是否显示" >
          <el-switch
    v-model="addForm.isShow"
    :active-value="1"
    :inactive-value="0">
  </el-switch>
  </el-form-item>
    </el-col>
  </el-row>
  <el-row :gutter="10">
    <el-col :span="14">
      <el-form-item label="摘要">
        <el-input
  type="textarea"
  :rows="3"
  placeholder="请输入内容"
  v-model="addForm.summary">
</el-input>
      </el-form-item>
    </el-col>
  </el-row>
<el-row :gutter="10">
  <el-col :span="8">
    <el-form-item label="封面照片">
      <el-upload
  class="upload-demo"
  ref="uploadImg"
  multiple
:on-success="uploadSuccess"
  :action="uploadUrl"
   :headers="uploadHeaders">
  <el-button size="small" type="primary">点击上传素材</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
</el-upload>
<img style="height: 150px" :src="addForm.coverImg" alt="">
    </el-form-item>
  </el-col>
</el-row>
<el-row :gutter="10">
  <el-col :span="24">
    <el-form-item label="切换富文本类型" label-width="120px">
      <el-radio-group v-model="addForm.editorType">
    <el-radio :label="0">富文本</el-radio>
    <el-radio :label="1">MarkDown</el-radio>

  </el-radio-group>
   <el-alert
    title="注意!切换编辑器会清空编辑内容"
    type="warning"
    style="display:inline"
     show-icon
     class="tips">
  </el-alert>

    </el-form-item>
  </el-col>
</el-row>
<div v-if="addForm.editorType===0">
  <el-upload
                  ref="uploadTi"
                  class="upload-demo"
                  :action="uploadUrl"
                  :headers="uploadHeaders"
                  multiple
                  :on-success="addTiImg"
                >
                  <el-button
                    size="mini"
                    type="primary"
                  >插入图片↓</el-button>
  </el-upload>
<TEditor ref="ted"></TEditor>
  </div>
  <div v-else>
    <MdEditor ref="md"></MdEditor>
  </div>
</el-form>
        </div >
        <el-row :gutter="10" style="margin-top:30px">
          <el-col :span="24" align="center">
            <el-button type="primary" @click="add('addForm')">立即更新</el-button>
          </el-col>
        </el-row>
      </el-card>
    </div>
</template>

<script>
import mixin from '@/minx/index'
import TEditor from '@/components/Tinymce/index'
import MdEditor from '@/components/MarkdownEditor'
import {updataArticle} from "@/api/article"
export default {
    props: {

    },
     mixins: [mixin],
    data() {
        return {
          addForm: {
          author:'',
          title:'',
          isShow:0,
          editorType:0,
          summary:'',
          coverImg:'',
          content1:'',
          content2:''
        },
 rules: {
          author: [
            { required: true, message: '请输入作者名字', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '请输入标题名称', trigger: 'blur' }
          ]
        }
        }
    },
    computed: {

    },
    created() {
      this.addForm=  JSON.parse(window.localStorage.getItem('article'))
      // console.log(this.addForm)
      if(this.addForm.content1===''){
        this.addForm.editorType===0
      }
    },
    mounted() {

    },
    watch: {

    },
    methods: {
      uploadSuccess(res,file){
        console.log(res)
       if(res.success){
         this.addForm.coverImg=res.data.fileUrl
         this.$refs.uploadImg.clearFiles()
         this.$message.success('上传图片成功')
       }

      },
      addTiImg(res){
        console.log(res)
        this.$refs.uploadTi.clearFiles()
        this.$refs.ted.imageSuccessCBK(res.data.fileUrl)
      },
      add(formName){
        // this.addForm.content1=this.$refs.ted.getContent()
        // // this.addForm.content2=this.$refs.md.getValue()
        // console.log(this.$refs.ted.getContent())

        this.addForm.content1=''
        if(this.addForm.editorType===0){
          this.addForm.content1=this.$refs.ted.getContent()
        }else{
          this.addForm.content1=this.$refs.md.getValue()
        }

 this.$refs[formName].validate((valid) => {
          if (valid) {
           updataArticle(this.addForm).then(res=>{
          if(res.success){
            this.$message.success('文章更新成功')
            this.$router.push({path:'/content/article'})
          }
        })
          } else {
           this.$message.error('文章更新失败')
            return false;
          }
        });

      }
    },
    components: {
      TEditor,
      MdEditor
    },
};
</script>

<style lang="scss" scoped>
.card-c{
  width: 80%;
  margin: 0 auto;
  .tips{
    margin-left: 30px;
  }
}
</style>
